#pragma once
#include <iostream>
#include <poll.h>
#include <string>
#include "Socket.hpp"
#include "log.hpp"

extern Log lg;

static const uint16_t defaultport = 8888;
static const int fd_num_max = 64;
static const int defaultfd = -1;
int nonevent = 0;
class PollServer
{
public:
    PollServer(uint16_t port = defaultport)
        : port_(port)
    {
        for (int i = 0; i < fd_num_max; i++)
        {
            event_fds[i].fd = defaultfd;
            event_fds[i].events = nonevent;
            event_fds[i].revents = nonevent;
        }
    }

    bool Init()
    {
        listensock_.Socket();
        listensock_.Bind(port_);
        listensock_.Listen();
        lg(Info, "Init success!");
    }
    void Accepter()
    {
        std::string clientip;
        uint16_t clientport;
        int new_fd = listensock_.Accept(&clientip, &clientport);
        if (new_fd < 0)
            return;
        lg(Info, "accept success,%s %d,fd:%d", clientip.c_str(), clientport, new_fd);
        int pos = 1;
        for (; pos < fd_num_max; pos++)
        {
            if (event_fds[pos].fd != defaultfd)
                continue;
            else
                break;
        }
        if (pos == fd_num_max)
        {
            lg(Warning, "server poll fd is full,close fd:%d", new_fd);
            close(new_fd);
        }
        else
        {
            event_fds[pos].fd = new_fd;
            event_fds[pos].events = POLLIN;
            event_fds[pos].revents = nonevent;
            Printfd();
        }
    }

    void Recver(int fd,int pos)
    {
        char buffer[1024];
        int n = read(fd, buffer, sizeof(buffer));
        if (n < 0)
        {
            lg(Warning, "read error,fdd is:%d", fd);
            close(fd);
            event_fds[pos].fd = defaultfd;
            ;
        }
        else if (n == 0)
        {
            lg(Info, "client quit,me too,close fd:%d", fd);
            close(fd);
            event_fds[pos].fd = defaultfd;
        }
        else
        {
            buffer[n] = 0;
            std::cout << "get a message: " << buffer << std::endl;
        }
    }

    void Dispatcher()
    {
        for (int i = 0; i < fd_num_max; i++)
        {
            int fd = event_fds[i].fd;
            if (fd == defaultfd)
                continue;

            if (event_fds[i].revents & POLLIN)
            {
                if (fd == listensock_.Fd())
                {
                    Accepter();
                }
                else
                {
                    Recver(fd,i);
                }
            }
        }
    }

    void Start()
    {
        event_fds[0].fd = listensock_.Fd();
        event_fds[0].events = POLLIN;
        int timeout = 3000;
        while (true)
        {
            int n = poll(event_fds, fd_num_max, timeout);
            switch (n)
            {
            case 0: // 超时
                std::cout << "time out" << std::endl;
                break;
            case -1: // 出错
                std::cout << "poll error" << std::endl;
                break;
            default: // 事件就绪
                std::cout << "get a new link" << std::endl;
                Dispatcher();
                break;
            }
        }
    }

    void Printfd()
    {
        std::cout << "Online fd: " << std::endl;
        for (int i = 0; i < fd_num_max; i++)
        {
            if (event_fds[i].fd == defaultfd)
                continue;
            std::cout << event_fds[i].fd << " ";
        }
        std::cout << std::endl;
    }

    ~PollServer()
    {
        listensock_.Close();
    }

private:
    Sock listensock_;
    uint16_t port_;
    struct pollfd event_fds[fd_num_max];
};